import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class Downloader {
	URL url;
	
	public Downloader(URL url) {
		this.url = url;
	}

	public String fetchAsString() {
		try {
			// System.out.println(url);

			URLConnection connection = getURLConnection();
			int status = ((HttpURLConnection) connection).getResponseCode();
			int contentLength = ((HttpURLConnection) connection).getContentLength();
			
			// We go only futher if received success code
			if(status != 200) {
				System.err.println("Twitter.com refused to give content");
				return null;
			}
			
			// We do not download file with too big size
			if(contentLength > 500000) { // 2581684
				System.err.println("Content-Length too long");
				return null;
			}
			
			// Update traffic
			Client.setTraffic(contentLength);
			
		    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		    String string = "";
		    String line;
		    while ((line = reader.readLine()) != null) {
		    	string = string + line;
		    }
		    reader.close();
		    
		    return string;
		    
		} catch (IOException e) {
			System.err.println("IOException on Downloader.java");
		}
		
		return null;
	}
	
	private URLConnection getURLConnection() {
		URLConnection connection = null;
		try {
			connection = url.openConnection();
			connection.setAllowUserInteraction(false);
			connection.setDoOutput(true);
			
			// Bypass 150-limit
			modifyHeader(connection);
			
		    // OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
			// writer.write(params);
			// writer.flush();
			
		    // writer.close();
		} catch (IOException e) {
			System.err.println("IOException on Downloader.java > getURLConnection()");
		}
		
		return connection;
	}
	
	public InputStream fetchAsStream() {
		URLConnection connection = getURLConnection();
		InputStream input = null;
		try {
			input = connection.getInputStream();
		} catch (IOException e) {
			System.err.println("IOException on Downloader.java > fetchAsStream()");
		}
	    return input;
	}
	
	private void modifyHeader(URLConnection connection) {
		connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_7) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.5 Safari/534.30");
		connection.addRequestProperty("Accept","application/json, text/javascript, */*");
		connection.addRequestProperty("Accept-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.3");
		connection.addRequestProperty("Accept-Language","en-US,en;q=0.8");
		connection.addRequestProperty("Connection","keep-alive");
		connection.addRequestProperty("Content-Type","application/x-www-form-urlencoded");
		connection.addRequestProperty("Host","api.twitter.com");
		connection.addRequestProperty("Referer","http://api.twitter.com/receiver.html");
		connection.addRequestProperty("X-PHX","true");
		connection.addRequestProperty("X-Requested-With","XMLHttpRequest");
	}
}
